package com.itheima.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.common.R;
import com.itheima.dto.SetmealDto;
import com.itheima.pojo.Category;
import com.itheima.pojo.Setmeal;
import com.itheima.pojo.SetmealDish;
import com.itheima.service.SetmealDishService;
import com.itheima.service.SetmealService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/setmeal")
@Slf4j
public class SetmealController {

@Resource
private SetmealDishService setmealDishService;
@Resource
private SetmealService setmealService;

//接口5：新增套餐接口
    @PostMapping
public R addSetmeal (@RequestBody SetmealDto setmealDto){

        int i = setmealService.addSetmeal(setmealDto);

        return i>0?R.success("ok"):R.error("no");
    }

//    接口二：套餐分页查询
    @GetMapping("/page")
    public R pageSetmeal(@RequestParam  Integer page ,Integer pageSize , String name){

        Page<Setmeal> setmealDtoPage = new Page<>(page,pageSize);

        LambdaQueryWrapper<Setmeal> qw = new LambdaQueryWrapper<>();
        qw.like(name!=null, Setmeal::getName,name);
        qw.orderByDesc(Setmeal ::getUpdateTime);
       setmealService.page(setmealDtoPage, qw);

        long total = setmealDtoPage.getTotal();
        List<Setmeal> records = setmealDtoPage.getRecords();

        int count = setmealService.count(qw);

        Map map = new HashMap<>();
        map.put("total",total);
        map.put("records",records);

        return R.success(map);
    }
//接口三：删除套餐
    @DeleteMapping
    public R delete(@RequestParam List<Long> ids){

        boolean b = setmealService.deleteSetmeal(ids);

        return b?R.success("ok"):R.error("no");
    }

//
    @PostMapping("/status/{status}")
    public R updateStatus (@PathVariable("status") Integer status, @RequestParam List<Long> ids){
        LambdaUpdateWrapper<Setmeal> qw = new LambdaUpdateWrapper<>();
        qw.in(Setmeal ::getId,ids);
        qw.set(Setmeal ::getStatus,status);
        boolean update = setmealService.update(qw);
        return update?R.success(""):R.error("");
    }

//接口五：修改套餐 接口1：根据套餐id查询套餐数据
    @GetMapping("/{id}")
    public R<SetmealDto>  updateSetmeal (@PathVariable("id") Long id ){
        Setmeal byId = setmealService.getById(id);
        LambdaUpdateWrapper<SetmealDish> lqw = new LambdaUpdateWrapper<>();
        lqw.eq(SetmealDish ::getDishId,id);
        List<SetmealDish> list1 = setmealDishService.list(lqw);
        SetmealDto setmealDto1 = new SetmealDto();
        setmealDto1.setSetmealDishes(list1);
        BeanUtils.copyProperties(byId,setmealDto1);
        return R.success(setmealDto1);
    }


    @PutMapping
    public R updateSetment2 (@RequestBody SetmealDto setmealDto){

        boolean b = setmealService.updateSetment(setmealDto);

        return b?R.success("ok"):R.error("no");

    }


//    接口三：根据套餐分类id查询该套餐分类下的套餐
        @GetMapping("/list")
    public R<List<Setmeal>>  select(long categoryId){
            LambdaQueryWrapper<Setmeal> sw = new LambdaQueryWrapper<>();
            sw.eq(Setmeal ::getCategoryId,categoryId);
            List<Setmeal> list = setmealService.list(sw);
            return R.success(list);
        }








}
